import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { UiService } from '../../ui.service';
import { GB } from '../../../environments/environment';
import { I18nService } from '../../core/i18n/i18n.service';
import { PickerService, ToptipsService } from 'ngx-weui';
import { ApiService } from 'src/app/core/api';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { StorageService } from 'src/app/core/sotrage/storage.service';

@Component({
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.less'],
  encapsulation: ViewEncapsulation.None,
})
export class InvestBalanceComponent implements OnInit {
  showUsdtPay=false;
  lang = this.sto.get('lang') ? this.sto.get('lang') : 'zh';
  investForm: FormGroup;
  radio =  [
    // { id: 1, name: 'USDT',img:'../../../assets/images/invest-page/USDT.png' , cash:'{"ja_JP":"米ドル","en_US":"d","zh_CN":"美元"}'}, 
    { id: 0, coinName: 'VISA',img:'../../../assets/images/invest-page/visa.png' , cash:'{"ja_JP":"日本円","en_US":"d","zh_CN":"日元"}'}, 
    // { id: 3, name: 'Jcash' ,img:'../../../assets/images/invest-page/Jcash.png', cash:"Jcash"}
  ];
  radioType=this.radio[0];
  msgData;
  address = ''
  constructor(
    private i: I18nService,
    private api: ApiService,
    private sto: StorageService,
    private route: ActivatedRoute,
    private router: Router,
    private fb: FormBuilder,
    private srv: PickerService,
    private toptips: ToptipsService,
  ) {
  }
  ngOnInit() {
    this.getPayList();
    this.investForm = this.fb.group(
      {
        amount: [null, [Validators.required]]
      }
    );
    // this.i.get([
    //   // 'InvestBalance-Page.radio1',
    //   // 'InvestBalance-Page.radio2'
    // ]).subscribe(res => {
    //   this.msgData = res;
    //   this.radioType = this.radio[0];
    // });
  };

  getPayList() {
    //暂时没有
    this.api.post('区块链地址列表',{}).then((e: any) => {
      let list  = e.data.addressList;
      for (let i = 0; i < list.length; i++) {
        let data = list[i];
        switch (data['coinName']) {
          case 'ETH':
            data['img'] = '../../../assets/images/invest-page/eth.jpeg' ;
            break;
          case 'BTC':
            data['img'] = '../../../assets/images/invest-page/btc.jpeg' ;
            break;
          case 'USDT':
            data['img'] = '../../../assets/images/invest-page/USDT.png' ;
            break;
        }
        data['cash'] = '{"ja_JP":"日本円","en_US":"d","zh_CN":"日元"}';
        data['id'] = i + 1;
        this.radio.push(data);
      }
    });
  }
  dialogShow = false;
  isError=true;
  rate = 1;
  coinName ;
  selCash(item) {
    this.coinName = item['coinName'];
    this.rate = item['rate'];
    this.address = item['address'];
  }
  toPay(form) {
    if(this.radio[0] != this.radioType){
      this.showUsdtPay=true;
    }else{
      if (form.valid) {
        this.api.post('用户充值', form.value).then((res: any) => {
          this.dialogShow=true;
          if(res.status == 0){
            this.isError=false;
          }else{
            this.isError=true;
          }
        });
      }
    }
  };
  onOutsize(bool) {
		this.dialogShow = bool;
	}
	chickOut() {
		this.dialogShow = false;
  }
  cloesUsdtPay(){
    this.showUsdtPay=false;
  }
  cancel() {
		this.dialogShow = false;
	}
  copyaddress() {
    const copyDom = document.getElementById('address');
    const range = document.createRange();
    range.selectNode(copyDom);
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(range);
    document.execCommand('copy');
    window.getSelection().removeAllRanges();
    this.toptips.success('复制成功！');
  }
}
